/**
 * 积分商城-今日任务
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link,
    hashHistory
} from 'react-router'

import Header from './Common/Header'
import LoadingBox from './Common/LoadingBox'
import {
    HttpService,
    getLocalTime,
    Tips,
    addStatistics,
    jxtUrl,
    sxtPageUrl,
    urlKey,
    showLoading,
    showHiding
} from '../Tools'

import position from '../Img/mall/position.png'
import anwesenheit from '../Img/mall/anwesenheit.png'
import score from '../Img/icon/score_index.png'
import classSpace from '../Img/mall/classSpace.png'

import comment from '../Img/mall/task/comment.png'
import phone from '../Img/mall/task/tel.png'
import task from '../Img/mall/task/task.png'
import photo from '../Img/mall/task/photo.png'
import login from '../Img/mall/task/login.png'
import notice from '../Img/mall/task/notice.png'
import head from '../Img/mall/task/head.png'
import info from '../Img/mall/task/info.png'
import infoshare from '../Img/mall/task/infoshare.png'
import addbaby from '../Img/mall/task/addbaby.png'

import imgBanner from '../Img/mall/banner.png'
import imgBanner2 from '../Img/mall/tasktoday.gif'

export default class TaskToday extends Component {
    constructor(props) {
        super(props);
        this.state = {
            taskData: []
        }
        window.taskStatus = () => {
            this.taskStatus()
        }
    }

    componentWillMount() {
        this.taskStatus()
    }

    //获取任务状态
    taskStatus() {
        showLoading()
        let rct = this;
        $.ajax({
            url: `/${sessionStorage.userrole=='家长'?'guardian':'teacherapp'}/credit/getCreditTasks`, //内网地址
            type: 'POST',
            data: `token=${sessionStorage.appToken}`,
            cache: false,
            // contentType: false, //不可缺
            // processData: false, //不可缺
            success: function(res) {
                showHiding()
                if (res.code == 20014) {
                    rct.setState({
                        taskData: res.data
                    })
                    return
                }
                Tips(res.msg)
            }
        })
    }


    //跳转处理
    taskLink(url, name) {
        if (url == '')
            return
        console.log(name);
        addStatistics(`/${sessionStorage.userrole=='家长'?'guardian':'teacherapp'}/credit/toComplete`);
        _hmt.push(['_trackEvent', 'page', 'tasktoday', name]);
        if (url.indexOf('@') > -1) { //需要APP处理的
            const taskName = url.split('@')[1],
                newUrl = url.split('@')[2];
            if (sessionStorage.weixin == 'true') {
                // if (taskName == '上传头像') {
                //     Tips('该任务只能在APP中完成')
                //     return;
                // }
                if (taskName == '发布班级通知' && sessionStorage.userrole != '班主任') {
                    Tips('该任务只能班主任完成')
                    return;
                }
                if (taskName == '查阅班级通知') {
                    location.href = `${jxtUrl}/jiaxiaotong/#news?${localStorage.jxtUrlData}`
                } else {
                    location.href = `${sxtPageUrl}/${newUrl}?appToken=${sessionStorage.appToken}`
                    // location.href = `/${newUrl}`
                }
            } else {
                if (sessionStorage.wherefrom == 'ios' && window.webkit) {
                    window.webkit.messageHandlers.taskFinish.postMessage(taskName);
                } else if (sessionStorage.wherefrom == 'android' && window.appweb) {
                    appweb.taskFinish(taskName)
                }
            }
            return
        }
        location.href = `${jxtUrl}/jiaxiaotong/#${url}?${localStorage.jxtUrlData}`
    }

    //app跳转处理
    urlGo() {
        if (urlKey('appback') != '') {
            location.href = '/jiaxiaotong/appback'
        } else {
            hashHistory.goBack(-1)
        }
    }

    taskTags(type) {
        let taskSpe = 0
        const tagList = this.state.taskData.map(item => {
            if (type != item.cycleType)
                return
            let imgUrl = login,
                taskUrl = '';
            let finish = item.achieveCount == item.haveAchieveCount
            switch (item.name) {
                case '发布作业':
                case '回复作业':
                    imgUrl = task
                    taskUrl = item.name == '发布作业' ? '/TaskPublish' : '/Task'
                    break;
                case '关爱学生':
                    imgUrl = phone
                    taskUrl = '/PhoneDirectories'
                    break;
                case '发布照片':
                case '评价相册':
                    imgUrl = photo
                    taskUrl = '/ClassAlbum'
                    break;
                case '评价学生':
                    imgUrl = comment
                    taskUrl = '/AddGrowthRecord'
                    break;
                case '分享资讯':
                    imgUrl = infoshare
                    taskUrl = '/info'
                    break;
                case '浏览资讯':
                    imgUrl = info
                    taskUrl = '/info'
                    break;
                case '发布班级通知':
                case '查阅班级通知':
                    imgUrl = notice
                    taskUrl = '@' + item.name + `@?state=teacher#/notice`
                    break;
                case '上传头像':
                    imgUrl = head
                    taskUrl = '@' + item.name + `@#/baby/${sessionStorage.babyid}`
                    break;
                case '添加孩子':
                    imgUrl = addbaby
                    taskUrl = '@' + item.name + `@#/addDevice`
                    break;
                default:
                    break;
            }

            return (
                <div className="take_list">
                    <img src={imgUrl} className="img" />
                    <span className="title">{item.name}<span>(<i>{item.haveAchieveCount}</i>/{item.achieveCount})</span></span>
                    <span className="punkte">+{item.num}</span>
                    <a className={finish?'task gary':'task'} onClick={finish?null:this.taskLink.bind(this,taskUrl,item.name)}>{finish?'已完成':'去完成'}</a>
                </div>
            )
        })
        return tagList
    }

    render() {
        const {
            taskData
        } = this.state
        return (
            <div className="task_today">
                <Header title="我的任务" addFn={this.urlGo.bind(this)} link={<a className='h_link refresh' onClick={this.taskStatus.bind(this)}></a>} />
                <div className="list_content">
                    <Link>
                        <img src={sessionStorage.userrole=='家长'?imgBanner:imgBanner2} />
                    </Link>
                    {taskData.length>0?<div className='tasknamebar'>今日任务</div>:null}
                    {this.taskTags(1)}
                    {taskData.length>0?<div className='tasknamebar'>个性任务</div>:null}
                    {this.taskTags(5)}
                </div>
            </div>
        )
    }
}
